var FactoryItem = require("FactoryItem");
var FileServe = require("FileServe");
var UIManage = require("UIManage");

var PopsInfo = require("PopsInfo");
cc.Class({
  extends: cc.Component,

  properties: {
    ItemLayout: cc.Node,
    MyBg: cc.Node,
  },

  start() {
    this.Disx = 72;
    this.Disy = 73;
    //   获取当前视图的可见区域尺寸
    let sysInfo = cc.view.getVisibleSize();
    //   分别存储屏幕的宽度和高度
    let width = sysInfo.width;
    let height = sysInfo.height;
    //   value 存储屏幕的宽高比
    var value = width / height;
    //   调整背景图的大小
    this.MyBg.width = width + 80 * value;
    this.MyBg.height = height + 80;
    this.ChildrenRankCom = cc.find("wx").getComponent("ChildrenRank");
  },

  //获取玩家道具数据
  getPopsInfo() {
    this.PopsList = new PopsInfo();
    var cList = FileServe.Instance.getPopsCache();

    if (cList != null) {
      this.PopsList.Diamond = cList.Diamond;

      if (FileServe.Instance.IsPassDay()) {
        this.PopsList.Hammer = 0;
        this.PopsList.Reset = 0;
        this.PopsList.Change = 0;
        cc.sys.localStorage.setItem("TimeGift", "1");
      } else {
        this.PopsList.Hammer = cList.Hammer;
        this.PopsList.Reset = cList.Reset;
        this.PopsList.Change = cList.Change;
      }
    } else {
      FileServe.Instance.IsPassDay();
      //新手礼包
      this.PopsList.Diamond = 50;
      this.PopsList.Hammer = 1;
      this.PopsList.Reset = 1;
    }
  },
  /**
   * 进行初始化和缓存检查。根据缓存的存在与否，决定显示不同的UI界面
   */
  realGamestart() {
    //   获取缓存数据
    var cache = FileServe.Instance.GetItemCache();
    var cache1 = FileServe.Instance.GetPlayInfo();
    //   检查缓存是否存在
    if (cache != null || cache1 != null) {
      // 如果任意一个缓存存在，则显示读取缓存的UI
      UIManage.Instance.ShowUIReadCache();
    } else {
      // 如果两个缓存都不存在，则显示游戏界面，并在游戏界面显示完成后进行初始化
      UIManage.Instance.ShowGameing(() => {
        this.Init();
      });
    }
  },
  /**
   * 用于在游戏启动时根据是否有缓存数据来决定初始化的方式
   * @param {*} iscache 用于指示是否需要检查缓存数据
   */
  GameStart(iscache) {
    UIManage.Instance.ShowGameing(() => {
      if (iscache) {
        var value = FileServe.Instance.GetItemCache();
        if (value == false || value == null) {
          this.Init();
        } else {
          this.CacheInit(value);
        }
      } else {
        this.Init();
      }
    });
  },
  /**
   * 用于初始化游戏的各种状态和元素。该方法接受一个可选参数 isEasy，用于指定游戏难度
   * @param {*} isEasy
   */
  Init(isEasy = false) {
    //   设置为 false，表示游戏未开始
    FactoryItem.Instance.IsGameStart = false;
    //   设置为 0，重置进度条
    FactoryItem.Instance.UIMianCom.BarScore.progress = 0;
    //   播放动画，并在动画结束后执行回调函数
    FactoryItem.Instance.UIMianCom.StartAnimation(() => {
      // 获取所有子节点
      var ItemlayoutChildren = this.ItemLayout.children;
      // 初始化 allVerticalList 数组，用于存储垂直列表
      FactoryItem.Instance.allVerticalList = new Array();
      // 生成一个 0 到 3 之间的随机整数，用于后续的权重选择
      var WeightIndex = Math.floor(Math.random() * 4);
      // 生成一个 0 到 99 之间的随机整数 value
      var value = Math.floor(Math.random() * 100);
      // 如果 value 小于 40，则生成另一个 0 到 99 之间的随机整数 index1，用于后续生成道具
      if (value < 40) {
        var index1 = Math.floor(Math.random() * 100);
      }
      //   遍历所有子节点，从 99 到 0
      for (var i = 99; i >= 0; i--) {
        //   worldPos1 获取子节点的世界坐标
        var worldPos1 = ItemlayoutChildren[i].convertToWorldSpaceAR(
          cc.v2(0, 0)
        );
        // 将世界坐标的 x 和 y 值保留一位小数
        var numx = Number(worldPos1.x.toFixed(1));
        var numy = Number(worldPos1.y.toFixed(1));
        //   创建一个新的 cc.v2 对象，表示世界坐标
        var worldPos = cc.v2(numx, numy);
        // 调用 LevelDiffcultValue 方法获取当前项的索引，根据难度进行调整
        var index = this.LevelDiffcultValue(WeightIndex, isEasy);
        //   调用 FactoryItem.Instance.CreatItem 方法创建一个新的游戏项
        var Item = FactoryItem.Instance.CreatItem(worldPos, i, index);
        //   获取游戏项的 Item 组件
        var ItemCom = Item.getComponent("Item");
        ItemCom.IsGameStart = false;

        // ItemCom._Leftstep = 0;
        // ItemCom._isAlreadyDetection = false;
        // ItemCom._PromptCoolTime = FactoryItem.Instance.constCoolTime;

        // 生成道具
        // 概率出现气泡红包
        if (i == index1) {
          ItemCom.ShowBox();
        }
        var time = 0.01 * (100 - i) + 0.1;
        var move = cc.moveTo(time, worldPos);
        Item.runAction(move);
      }
      FactoryItem.Instance.setItemXY();
      FactoryItem.Instance.GetDis();
    });
  },
  /**
   * 用来为游戏或其他应用程序生成具有一定概率分布的随机数
   */
  LevelDiffcultValue(WeightIndex, isEasy = false) {
    if (isEasy) {
      var DiffWeight = 0.25;
    } else {
      var DiffWeight = 0.1;
      var level = FactoryItem.Instance.UIMianCom._PlayInfo._Level;
      if (level <= 10) {
        DiffWeight = 0.2;
      } else if (level > 10 && level <= 20) {
        DiffWeight = 0.15;
      } else {
        DiffWeight = 0.1;
      }
    }
    if (WeightIndex == -1) {
      var index = Math.floor(Math.random() * 5);
    } else {
      var randomValue = Math.random() * 1;
      if (randomValue < DiffWeight) {
        var index = WeightIndex;
      } else if (randomValue >= DiffWeight && randomValue < 2 * DiffWeight) {
        var index = WeightIndex + 1;
      } else {
        var index = Math.floor(Math.random() * 5);
      }
    }
    return index;
  },

  CacheInit(cache) {
    var cache1 = cc.sys.localStorage.getItem("Vesion");
    var myVersion = this.ChildrenRankCom._version;
    if (cache1 == null || cache1 == "") {
      this.Init();
      cc.sys.localStorage.setItem("Vesion", myVersion);
    } else {
      if (Number(cache1) == myVersion) {
        FactoryItem.Instance.IsGameStart = false;
        var LayoutChildren = this.ItemLayout.children;
        FactoryItem.Instance.UIMianCom.StartAnimation(() => {
          for (var i = cache.length - 1; i >= 0; i--) {
            //var posx = cc.v2(cache[i].X,cache[i].Y);
            if (cache[i]._X == undefined) {
              var posx = cc.v2(cache[i].X, cache[i].Y);
            } else {
              var index = 9 - cache[i]._X + cache[i]._Y * 10;
              //var pos = LayoutChildren[index].getPosition();
              var posx = LayoutChildren[99 - index].convertToWorldSpaceAR(
                cc.v2(0, 0)
              );
            }
            var colorType = cache[i]._ColorType;
            var _active = cache[i].active == 1 ? true : false;
            var _isDestory = cache[i]._isDestory == 0 ? true : false;
            var _isHasBox = cache[i]._isHasBox;
            var Item = FactoryItem.Instance.CacheCreatItem(
              posx,
              i,
              colorType,
              _isDestory,
              _isHasBox
            );
            Item.active = _active;

            var time = 0.01 * (100 - i) + 0.1;
            var move = cc.moveTo(time, posx);

            Item.runAction(move);
          }
          FactoryItem.Instance.test();
          FactoryItem.Instance.GetDis();
        });
      } else {
        this.Init();
        cc.sys.localStorage.setItem("Vesion", myVersion);
      }
    }
  },
});
